﻿(function ($) {
    $.fn.accordion = function (option) {
        option = $.extend({
            headerTag: ".header",
            contentTag: ".content",
            itemTag: "div",
            expandItem: 0
            /*
            expandItem:0 , .item1
            */
        }, option);
        return this.each(function () {
            var view = $(this);
            var settings = $.extend({}, option);
            var helper = new $.accordionHelper(view, settings);
            view.helper("accordion", helper);
            helper.init();
        });
    };

    $.accordionHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;
        this.items = null;
        var _this = this;
        this.init = function () {
            this.jo.layout(function () {
                _this.layout();
            });
            this.jo.addClass("accordion");
            this.items = this.jo.children(this.setting.itemTag);
            this.items.each(function () {
                $(this).children(_this.setting.headerTag).addClass("header");
                $(this).children(_this.setting.contentTag).addClass("content");
                $(this).children(_this.setting.headerTag).on("click", function () {
                    var _item = $(this).parent();
                    var _index = _this.items.index(_item);

                    /*
                    added by zdh 2013-10-23
                    增加 点击“-”收拢  功能
                    */
                    var _itemcontent = _item.children(_this.setting.contentTag + ":visible");

                    if (_itemcontent.size() == 0) {
                        _this.expand(_index);
                    } else {
                        _this.expand(-1);
                    }
                });
            });
            this.expand(this.setting.expandItem);
            this.jo.layout();
        };
        this.expand = function (selector) {
            if (!isNaN(selector)) {
                selector = ":eq(" + selector + ")";
            }
            this.items.each(function () {
                $(this).children(_this.setting.contentTag).hide();
                $(this).removeClass("accordionexpand");
            });
            if (selector != ":eq(-1)") {
                this.jo.children(selector).addClass("accordionexpand");
                this.jo.children(selector).children(_this.setting.contentTag).show();
            }
            this.layout();
        }
        this.layout = function () {
            var height = this.jo.height();
            var useheight = 0;
            this.jo.children(":not(.accordionexpand)").each(function () {
                useheight += $(this).outerHeight();
            });
            var expanditem = this.jo.children(".accordionexpand");

            useheight += expanditem.outerHeight() - expanditem.height();
            useheight += expanditem.children(_this.setting.headerTag).outerHeight();
            useheight += expanditem.children(_this.setting.contentTag).outerHeight() - expanditem.children(_this.setting.contentTag).height();
            var odd = height - useheight;
            expanditem.children(_this.setting.contentTag).height(odd);
        }
    }
})(jQuery);